package com.beiing.tianshuai.tianshuai.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;

import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/9/28 0028.
 */

public class CheckVersionDialog extends Dialog{

    private TextView mTvVersionName;
    private TextView mTvUpdateContent;
    private Button mBtnUpdateNow;
    private Button mBtnUpdateBackground;
    private Button mBtnCancel;

    private Context mContext;
    private OnCloseListener mListener;
    private String versionName;
    private String content;

    public CheckVersionDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public CheckVersionDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    public CheckVersionDialog(Context context, int themeResId, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.mListener = listener;
    }

    protected CheckVersionDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public CheckVersionDialog setVersionName(String versionName) {
        this.versionName = versionName;
        return this;
    }

    public CheckVersionDialog setContent(String content) {
        this.content = content;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_check_version);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        mTvVersionName = (TextView) findViewById(R.id.tv_version_name);
        mTvUpdateContent = (TextView) findViewById(R.id.tv_update_content);
        mBtnUpdateNow = (Button) findViewById(R.id.btn_update_now);
        mBtnUpdateBackground = (Button) findViewById(R.id.btn_update_background);
        mBtnCancel = (Button) findViewById(R.id.btn_cancel);
        mTvUpdateContent.setMovementMethod(ScrollingMovementMethod.getInstance());
        mBtnUpdateNow.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (mListener != null) {
                    mListener.onClick(CheckVersionDialog.this, true, v.getId());
                }
            }
        });
        mBtnUpdateBackground.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (mListener != null) {
                    mListener.onClick(CheckVersionDialog.this, true, v.getId());
                }
            }
        });
        mBtnCancel.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (mListener != null) {
                    mListener.onClick(CheckVersionDialog.this, false, v.getId());
                }
            }
        });
        if (!TextUtils.isEmpty(versionName)) {
            mTvVersionName.setText(versionName);
        }
        if (!TextUtils.isEmpty(content)) {
            mTvUpdateContent.setText(content);
        }
    }

    public interface OnCloseListener{
        void onClick(Dialog dialog, boolean confirm, int id);
    }
}
